package cn.wolfcode.consumer._03_consumemode;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;


/**
 * BROADCASTING:广播模式  会给所有的消费者发一份
 * CLUSTERING: 集群模式 只会有一个消费者消费信息 默认值
 * consumer.setMessageModel(MessageModel.BROADCASTING);
 */
public class Consumer2 {
    public static void main(String[] args) throws MQClientException {
        //1.创建一个拉取消息的消费者对象
        DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer("api-producer-demo");
        //设置消费模式
        //defaultMQPushConsumer.setMessageModel(MessageModel.CLUSTERING);//集群消费
        defaultMQPushConsumer.setMessageModel(MessageModel.BROADCASTING);//广播消费
        //2.设置名字地址
        defaultMQPushConsumer.setNamesrvAddr("127.0.0.1:9876");
        //3.绑定消息的主题topic
        defaultMQPushConsumer.subscribe("05-sendmode-boot", "*");
        //4.消费者监听消息的
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("2消费线程:"+Thread.currentThread().getName()+",消息ID:"+messageExt.getMsgId()+",消息内容:"+new String(messageExt.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5.启动消费者
        defaultMQPushConsumer.start();

    }
}
